package view;

import result.Result;
import util.Client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author 化尘成影
 * @Description
 * @date 2022/6/27 13:33
 */
public class Register extends JFrame {
    public Register() {
        super();
        initFrame();
    }

    public void initFrame() {
        setTitle("五子棋[注册]");
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        MyJPanel myJPanel = new MyJPanel(this);
        this.setLocationRelativeTo(null);
        setContentPane(myJPanel);

    }

    public class MyJPanel extends JPanel implements MouseListener {
        Font font = new Font(Font.SERIF, Font.PLAIN, 30);
        Font font2 = new Font(Font.SERIF,Font.PLAIN,20);
        Register regForm;
        JLabel l1 = new JLabel("用户名");
        JLabel l2 = new JLabel("密码");
        JLabel l4 = new JLabel("确认密码");
        JLabel l3 = new JLabel("注册界面");
        JTextField username = new JTextField();
        JPasswordField password = new JPasswordField();
        JPasswordField password2 = new JPasswordField();
        JButton register = new JButton("注册");
        JButton back = new JButton("返回");

        ImageIcon icon;
        Image img;

        public MyJPanel(Register regForm) {
            super();
            setLayout(null);
            this.regForm = regForm;
            this.addMouseListener(this);

            //设置组件信息
            l1.setBounds(100, 110, 100, 50);
            l1.setFont(font2);
            username.setBounds(200, 125, 200, 25);
            l2.setBounds(100, 180, 100, 50);
            l2.setFont(font2);
            password.setBounds(200, 200, 200, 25);
            l4.setBounds(100,250,100,50);
            l4.setFont(font2);
            password2.setBounds(200,275,200,25);
            l3.setBounds(20, 50, 450, 50);
            l3.setFont(font);
            register.setBounds(100, 320, 100, 50);
            register.setFont(font2);
            back.setBounds(300, 320, 100, 50);
            back.setFont(font2);

            //添加到面板去
            add(l1);
            add(l2);
            add(l3);
            add(l4);
            add(username);
            add(password);
            add(register);
            add(password2);
            add(back);

            //获取背景图
            icon = new ImageIcon(getClass().getResource("/image/a.png"));
            img = icon.getImage();

            //注册按钮响应
            register.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        //判断是否有输入
                        if (username.getText().equals("")) {
                            JOptionPane.showMessageDialog(regForm, "用户名不能为空");
                        } else if (password.getText().equals("")||password2.getText().equals("")) {
                            JOptionPane.showMessageDialog(regForm, "密码不能为空");
                        } else if (!password.getText().equals(password2.getText())){
                            JOptionPane.showMessageDialog(regForm,"密码不一致");
                        }else {
                            String name = username.getText();
                            String pass = password.getText();

                            HashMap<String, String> map = new HashMap<>();
                            map.put("username",name);
                            map.put("password",pass);
                            Result result = Client.request("register", map,true);

                            if (result.getCodeMsg().getCode() == 200) {
                                JOptionPane.showMessageDialog(regForm, "注册成功，请重新登录");
                                regForm.dispose();
                                Login login = new Login();
                                login.setVisible(true);

                            } else if(Objects.equals(result.getCodeMsg().getCode(),404)){
                                JOptionPane.showMessageDialog(regForm, result.getCodeMsg().getMsg());
                                regForm.dispose();
                                new Login().setVisible(true);
                            }else {
                                JOptionPane.showMessageDialog(regForm, result.getCodeMsg().getMsg());
                            }
                        }
                    } catch (Exception exception){
                        JOptionPane.showMessageDialog(regForm,"503：服务器异常");
                    }

                }
            });

            //返回按钮响应
            back.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Login login = new Login();
                    regForm.dispose();
                    login.setVisible(true);
                }
            });
        }

        //添加背景图
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
        }

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }
}
